library(stringr)
library(ggplot2)
library("sos");
library("sitools")
library(scales)

setwd('/home/song/project/going/neo4j/demo/logs');
files <- list.files()
#head(files)


extraVarFromFile = function(fileName,dbName){
  content = readChar(fileName, file.info(fileName)$size);
  patternLines = c(
    "(\\d+) INFO\\[Neo4jTemporal-oneFile-importer\\] PROGRESS: (\\d+\\.\\d+) % =======================\\r\\n",
    "\\d+ INFO\\[Neo4jTemporal-oneFile-importer\\] TOTAL: read (\\d+) line, read (\\d+) MB, write (\\d+) MB to DB, commit (\\d+) transactions\\r\\n",
    "\\d+ INFO\\[Neo4jTemporal-oneFile-importer\\] AVG: in (\\d+)s: read (\\d+) line/s, read (\\d+) KB/s, write (\\d+) KB/s, commit (\\d+\\.\\d+) tx/s  CURRENT: (\\d+) line/tx\\r\\n"
  );
  patterns = paste(patternLines,collapse="");
  result = str_match_all(content, patterns);
  
  T.tmp0 = data.frame(result)
  T.tmp1 = data.frame(T.tmp0[-1,-1], stringsAsFactors = FALSE)
  T.index = sapply(T.tmp1, is.factor)
  T.tmp1[T.index] = lapply(T.tmp1[T.index], function(x) as.numeric(as.character(x)))
  T.tmp1["db"]=dbName
  T.tmp1
}


fileName="20160228-neo4jtemporal-oneFileImort-on-server.log";
T.tmp1 = extraVarFromFile(fileName,"TGraph")
T.tmp1 = T.tmp1[seq(1,498,20),]

fileName="20160229-neo4jArraySimulation-onefileimport-on-server-abort.log";
T.tmp2 = extraVarFromFile(fileName,"Neo4j")

#T.tmp = rbind(T.tmp1,T.tmp2)
T.tmp = rbind(T.tmp2,T.tmp1)

# x:time,y:tx commited
P.1 = ggplot(T.tmp, aes(x=X2/1000,y=X7,colour=db,shape=db)) + geom_line() + geom_point(size=3)+
  theme_bw()+ theme(legend.justification=c(0,1), legend.position=c(0, 1),
                    legend.title=element_blank(),
                    legend.key=element_blank()
                    #axis.title=element_text(size=14)) + #,face="bold"
  )+
  scale_x_continuous(limits=c(0,81500), labels=comma) + 
  scale_y_continuous(limits=c(0,40000), labels=f2si) +
  #scale_y_continuous(trans='log') +
  labs(#title="Compare: committed transaction count",
    x="Time(s)",y="Number of transactions",fill="")

rm(list=setdiff(ls(), c("P.4","P.1","P.2","P.3")))